package com.data.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.base.dao.TemplateDaoImpl;
import com.data.entity.ChargeDay;
import com.data.entity.ChargeMin;

@Repository("chargeDao")
public class ChargeDaoImpl extends TemplateDaoImpl<ChargeDay, Integer>implements ChargeDao{
	/**
	 * 根据设备或生产线编号查询一段时间的总电度电费
	 * @param id设备或生产线id
	 * @param startDate
	 * @param endDate
	 * @param isLine
	 * @return
	 */
	@Override
	public ChargeDay getTotalChargesByDay(int id, String startDate,String endDate, String isLine) {
		Map<String, Object> params=new HashMap<String, Object>();
		StringBuffer sb=new StringBuffer("select  COALESCE(SUM(relativeComp),0)relativeComp, COALESCE(SUM(charge),0) charge  from chargeDay  where ");
		if(isLine.equals("Y")){
			sb.append(" lineId=:id ");
		}else{
			sb.append(" equId=:id ");
		}
		sb.append(" and DATE_FORMAT(belongDate,'%Y-%m-%d')>=:startDate and DATE_FORMAT(belongDate,'%Y-%m-%d')<=:endDate");
		params.put("id", id);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		return jt.queryForObject(sb.toString(), params, ParameterizedBeanPropertyRowMapper.newInstance(ChargeDay.class));	
	}
	/**
	 * 根据当前小时时间查询当前小时总电度，总电费
	 * @param id
	 * @param startTime
	 * @param endTime
	 * @param isLine
	 * @return
	 */
	@Override
	public ChargeMin getHourChargesByHour(int id, String startTime,String endTime, String isLine) {
		Map<String, Object> params=new HashMap<String, Object>();
		StringBuffer sb=new StringBuffer("select  COALESCE(SUM(relativeComp),0)relativeComp, COALESCE(SUM(charge),0) charge  from chargeMin  where ");
		if(isLine.equals("Y")){
			sb.append(" lineId=:id ");
		}else{
			sb.append(" equId=:id ");
		}
		sb.append(" and DATE_FORMAT(belongTime,'%Y-%m-%d %H:%i')>=:startTime and DATE_FORMAT(belongTime,'%Y-%m-%d %H:%i')<=:endTime");
		params.put("id", id);
		params.put("startTime", startTime);
		params.put("endTime", endTime);
		return jt.queryForObject(sb.toString(), params, ParameterizedBeanPropertyRowMapper.newInstance(ChargeMin.class));	
	}
	/**
	 * 查询归属天的电费所有数据
	 * @param id
	 * @param belongDate
	 * @param isLine
	 * @return
	 */
	@Override
	public List<ChargeMin> getChargeMinsByDay(int id, String belongDate,String isLine) {
		StringBuffer sb=new StringBuffer("select belongTime,relativeComp,charge from chargeMin where");
		if(isLine.equals("Y")){
			sb.append(" lineId=:id ");
		}else{
			sb.append(" equId=:id ");
		}
		sb.append(" and DATE_FORMAT(belongTime,'%Y-%m-%d')=:belongDate order by belongTime asc");
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("id", id);
		params.put("belongDate", belongDate);
		return jt.query(sb.toString(), params, ParameterizedBeanPropertyRowMapper.newInstance(ChargeMin.class));
	}
}
